# -------------------------------------------------------------------
#       -*- coding: utf-8 -*-
#   @Project    :   spider
#   @File       :   45_2_协程.py
#   @Author     :   WANGYU
#   @Time       :   2021-09-04 08:48:59
#   @Software   :   PyCharm
#   @Desc       :   
# -------------------------------------------------------------------

import asyncio
import time


async def func():
    print('hello i am func')
    # time.sleep(2)  # 当程序出现了同步操作的时候，异步就中断
    await asyncio.sleep(2)  # 异步操作的代码
    print('hello i am func')


async def func1():
    print('hello i am func1')
    # time.sleep(2)
    await asyncio.sleep(3)  # 异步操作的代码
    print('hello i am func1')


async def func2():
    print('hello i am func2')
    # time.sleep(2)
    await asyncio.sleep(4)  # 异步操作的代码
    print('hello i am func2')


if __name__ == '__main__':
    # g = func()  # 此时的函数是异步协程函数，此时函数执行得到的是一个协程对象
    # print(g)
    # asyncio.run(g) #协程程序运行需要asyncio模块支持
    #
    f = func()
    f1 = func1()
    f2 = func2()
    tasks = [f, f1, f2]

    t1 = time.time()

    # 一次性启动多个任务（协程）
    asyncio.run(asyncio.wait(tasks))

    t2 = time.time()
    print(t2 - t1)
